#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define CSIZE 32


int main(int argc,const char*  argv[]){

    if(argc != 2){
        return -1;
    }

    char buf[32]="";
    if(mkfifo("./fifo",0666) < 0){
        if(errno == EEXIST){
            printf("file exist");
        }else{
            perror("mkfifo err");
            return -1;
        }
    }
    printf("mikfifo sucess\n");
    int fd_r = open(argv[1],O_RDONLY);
    if(fd_r < 0){
        perror("open err");
        return -1;
    }
    int fd_w = open("./fifo",O_WRONLY);
    if(fd_w < 0){
        close(fd_r);
        perror("open err");
        return -1;
    }
    int n=0;
    while(1){
        n=read(fd_r,buf,CSIZE);
        if(n == -1){
            perror("read err");
            break;
        }
        if(write(fd_w,buf,n) == -1){
            perror("write err");
            break;
        }
        

        if(n < CSIZE){
            break;
        }
        
    }
    

    close(fd_r);
    close(fd_w);

    
    

    return 0;
}